import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;

/**
 * 算式抽象类
 *
 * @author Li Sipeng
 * @version 4.1 on   2022-11-14 12:05
 */
public abstract class Operation implements Serializable {
    private static final long serialVersionUID = 1244232321L;
    static final int max_value = 100;
    static final int min_value = 0;
    private int left_operand = 1,right_operand = 0;
    private char operator = '+';
    private int value = 0;
    private int inputValue = 0;

    public int getInputValue() {
        return inputValue;
    }

    public void setInputValue(int inputValue) {
        this.inputValue = inputValue;
    }

    public int getLeft_operand() {
        return left_operand;
    }

    public int getRight_operand() {
        return right_operand;
    }

    public char getOperator() {
        return operator;
    }

    public int getValue() {
        return value;
    }

    public void setLeft_operand(int left_operand) {
        this.left_operand = left_operand;
    }

    public void setRight_operand(int right_operand) {
        this.right_operand = right_operand;
    }

    public void setOperator(char operator) {
        this.operator = operator;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public String toString(){
        String s;
        String Charop = String.valueOf(operator);
        s = left_operand+Charop+right_operand+"="+value;
        return s;
    }

    /**
     *   随机生成加减符号
     *
     * @return 随机返回一个加减符号+-
     */
    public abstract char generateOpator();

    /**
     * 产生一个不超过 max_value的随机数
     *
     * @param max_value
     * @return
     */
    public static int generateOperand(int max_value) {
        Random random = new Random();
        return random.nextInt(max_value+1);
    }

    /**
     * 修改Operation实体类中的构成加法算式的属性
     *
     * */
    public void generateAddOperand(){
        do {
            left_operand = generateOperand(max_value);// 产生一个不超过max_value的操作数
            right_operand = generateOperand(max_value);// 产生一个不超过max_value的操作数
            value = left_operand + right_operand;
        }while(value > max_value);
    }

    /**
     * 修改Operation实体类中的构成加法算式的属性
     *
     * */
    public void generateSubOperand(){
        do {
            left_operand = generateOperand(max_value);// 产生一个不超过max_value的操作数
            right_operand = generateOperand(max_value);// 产生一个不超过max_value的操作数
            value = left_operand - right_operand;
        }while(value < min_value);
    }

    /**
     * 判断算式在习题中是否重复
     *
     * @param eq： BinaryOperation算式对象
     * @param exercise：习题集合，[{},{},{}...]
     * @param index：目前习题中算式的个数（游标）
     * @return：返回布尔值，重复返回1；不重复返回0.
     */
    public static boolean occursIn(Operation eq, ArrayList<Operation> exercise, int index) {
        if (exercise.size() == 0) return false;
        boolean found = false;
        for(int i = 0; i < index; i++) {
            if( isEqual(eq, exercise.get(i))) {
                found = true;
                break;
            }
        }
        return found;
    }
    /**
     * 判断两个算式是否相等,静态方法
     * @param eq1：第一个Operation算式对象
     * @param eq2：第二个Operation算式对象
     * @return：如果相等，返回1；如果不相等，返回0
     */
    public static boolean isEqual(Operation eq1, Operation eq2) {
        return (eq1.left_operand == eq2.left_operand && eq1.right_operand == eq2.right_operand && eq1.operator == eq2.operator)
                || (eq1.left_operand == eq2.right_operand && eq1.right_operand == eq2.left_operand && eq1.operator == eq2.operator);
    }


}
